package com.ec.system.modules.sys.feign;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ec.common.constant.CommonConstant;
import com.ec.core.common.core.api.ResponseMsg;
import com.ec.core.common.core.api.Result;
import com.ec.core.common.utils.BeanUtilEx;
import com.ec.core.common.utils.HttpServletUtil;
import com.ec.system.api.modules.sys.bo.SysUserLoginBO;
import com.ec.system.api.modules.sys.dto.SysUserLoginDTO;
import com.ec.system.api.modules.sys.feign.ISysUserFeign;
import com.ec.system.api.modules.sys.vo.SysRoleVO;
import com.ec.system.api.modules.sys.vo.SysUserLoginVO;
import com.ec.system.common.enums.RespResultEnum;
import com.ec.system.modules.sys.entity.SysRole;
import com.ec.system.modules.sys.entity.SysUser;
import com.ec.system.modules.sys.service.ISysRoleService;
import com.ec.system.modules.sys.service.ISysUserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Eric Lee
 * @Description:
 */
@RestController
public class SysUserFeign implements ISysUserFeign {
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private ISysRoleService sysRoleService;

    @Override
//    @PostMapping(API_PREFIX + "/login")
    @PostMapping("/user-feign/login")
    public Result<SysUserLoginVO> sysUserLogin(SysUserLoginDTO loginDTO) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        SysUserLoginBO loginBO = sysUserService.querySysUserInfo(loginDTO);
        if (Objects.isNull(loginBO)) {
            return ResponseMsg.error(RespResultEnum.ACCOUNT_IS_NOT_EXISTS_ERROR);
        }
        if (!encoder.matches(loginDTO.getUserPassword(), loginBO.getUserPassword())) {
            return ResponseMsg.error(RespResultEnum.ACCOUNT_OR_PASSWORD_CHECK_FAILED_ERROR);
        }
        if (loginBO.getUserStatus() == CommonConstant.DATA_STATUS_FORBIDDEN) {
            return ResponseMsg.error(RespResultEnum.ACCOUNT_FORBIDDEN_ERROR);
        }
        if (loginBO.getTenantStatus() == CommonConstant.DATA_STATUS_FORBIDDEN) {
            return ResponseMsg.error(RespResultEnum.TENANT_FORBIDDEN_ERROR);
        }
        //保存登录信息
        String ipAddr = HttpServletUtil.getClientIp();
        loginBO.setUserLoginIp(ipAddr);
        LambdaUpdateWrapper<SysUser> updateWrapper = Wrappers.<SysUser>lambdaUpdate()
                .set(SysUser::getLastLoginTime, LocalDateTime.now())
                .set(SysUser::getUserLoginIp, ipAddr)
                .eq(SysUser::getUserId, loginBO.getUserId());
        sysUserService.update(updateWrapper);

        //角色
        List<SysRole> sysRoles = sysRoleService.queryRoleByUserId(loginBO.getUserId());
        List<SysRoleVO> roles = sysRoles.stream().map(e -> {
            SysRoleVO vo = new SysRoleVO();
            vo.setRoleCode(e.getRoleCode());
            vo.setRoleName(e.getRoleName());
            vo.setRoleId(e.getRoleId());
            return vo;
        }).collect(Collectors.toList());
        SysUserLoginVO vo = new SysUserLoginVO();
        BeanUtilEx.copyProperties(loginBO, vo);
        vo.setRoles(roles);

        return ResponseMsg.success(vo);
    }


}
